package com.resou.reader.tinder.favorites;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.resou.reader.R;
import com.resou.reader.base.ResouBaseActivity;
import com.resou.reader.bookdetail.BookDetailActivity;
import com.resou.reader.data.tinder.tinder.CartBook;
import com.resou.reader.tinder.SwipeActivity;
import com.resou.reader.utils.ErrorUtils;
import com.resou.reader.utils.UserUtil;

import java.util.List;

import butterknife.BindView;

public class FavoritesActivity extends ResouBaseActivity<FavoritesPresenter> implements IFavoritesView, FavoritesAdapter.OnItemSelectedListener, View.OnClickListener {

    @BindView(R.id.tv_toolbar_title)
    TextView tvToolbarTitle;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;
    @BindView(R.id.tv_add_to_bookshelf)
    TextView tvAddToBookshelf;
    @BindView(R.id.tv_delete)
    TextView tvDelete;
    @BindView(R.id.ll_bottom_bar)
    LinearLayout llBottomBar;
    @BindView(R.id.progress_bar)
    LinearLayout progressBar;
    @BindView(R.id.view_divider)
    View viewDivider;
    @BindView(R.id.progress_bar_text)
    TextView progressBarText;
    @BindView(R.id.fl_error_container)
    FrameLayout flErrorContainer;
    @BindView(R.id.tv_right_top_btn)
    TextView tvRightTopBtn;
    private FavoritesAdapter mAdapter;
    protected boolean isInEditMode = false;
    private FavoritesPresenter mPresenter;
    private static final String TAG = "FavoritesActivity-App";

    public static void startActivity(Context context) {
        Intent starter = new Intent(context, FavoritesActivity.class);
        context.startActivity(starter);
    }

    @Override
    protected void onStart() {
        super.onStart();
        //给presenter传递视图
        mPresenter.setView(this);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int itemId = item.getItemId();
        if (itemId == android.R.id.home) {
            if (isInEditMode) {
                exitEditMode();
            } else {
                finish();
            }
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void retryRefreshData() {

    }

    @Override
    protected void initStatusBar() {

    }

    @Override
    protected void initPresenter() {
        //实例化presenter
        mPresenter = new FavoritesPresenter(this);
    }

    @Override
    protected void initView() {
        //设置布局管理器
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        //创建适配器
        mAdapter = new FavoritesAdapter(this);
        recyclerView.setAdapter(mAdapter);
        //设置监听器
        tvDelete.setOnClickListener(this);
        tvRightTopBtn.setOnClickListener(this);
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_favorites;
    }

    @Override
    public void onBackPressed() {
        //按下返回键后先关闭编辑模式
        if (isInEditMode) {
            exitEditMode();
        } else {
            super.onBackPressed();
        }
    }

    private void startEditMode() {
        isInEditMode = true;
        tvRightTopBtn.setText("全选");
        mAdapter.notifyDataSetChanged();
        viewDivider.setVisibility(View.VISIBLE);
        llBottomBar.setVisibility(View.VISIBLE);
    }

    private void exitEditMode() {
        isInEditMode = false;
        tvRightTopBtn.setText("编辑");
        mAdapter.notifyDataSetChanged();
        mAdapter.clearSelected();
        onItemSelected();
        viewDivider.setVisibility(View.GONE);
        llBottomBar.setVisibility(View.GONE);
    }

    @Override
    public void onItemSelected() {
        int selectedCount = mAdapter.getSelectedItemCount();
        if (selectedCount == 0) {
            tvDelete.setText("删除");
            tvDelete.setTextColor(getResources().getColor(R.color.text_view_default_color));
            tvAddToBookshelf.setTextColor(getResources().getColor(R.color.text_view_default_color));
        } else {
            tvAddToBookshelf.setTextColor(getResources().getColor(R.color.theme_color));
            tvDelete.setTextColor(getResources().getColor(R.color.theme_color));
            tvDelete.setText("删除(已选" + selectedCount + "项)");
        }
    }

    @Override
    public void onItemDeleted(String bookId) {
        mPresenter.delete(UserUtil.getToken(), bookId);
    }

    @Override
    public void onItemClicked(CartBook book) {
        BookDetailActivity.startDetailActivity(this, book.getId());
    }

    @Override
    public void onClick(View v) {
        if (v == tvDelete) {
            if (mAdapter.getSelectedItemCount() > 0) {
                mAdapter.deleteSelectedItems();
                onItemSelected();
                if (mAdapter.getItemCount() == 0) {
                    tvRightTopBtn.setText("");
                }
            }
        }
        if (v == tvRightTopBtn) {
            if (!isInEditMode) {
                //如果不处于编辑模式下
                startEditMode();
            } else {
                //处于编辑模式下
                if (mAdapter.isSelectedAll()) {
                    //已经全选，则反选
                    mAdapter.setSelectedAll(false);
                    tvRightTopBtn.setText("全选");
                } else {
                    //已经反选，则全选
                    mAdapter.setSelectedAll(true);
                    tvRightTopBtn.setText("反选");
                }
                onItemSelected();
            }
        }
    }

    @Override
    public void initData() {
        //获取数据列表
        mPresenter.getCartBookList(UserUtil.getToken());
    }

    @Override
    public void showContent() {

    }

    @Override
    public void showProgress() {
        progressBar.setVisibility(View.VISIBLE);
        tvRightTopBtn.setVisibility(View.GONE);
        recyclerView.setVisibility(View.GONE);
        llBottomBar.setVisibility(View.GONE);
        viewDivider.setVisibility(View.GONE);
    }

    @Override
    public void showError(Throwable throwable) {

    }

    @Override
    public void showContent(List<CartBook> books) {
        if (books != null) {
            mAdapter.setData(books);
        }
        recyclerView.setVisibility(View.VISIBLE);
        tvRightTopBtn.setVisibility(View.VISIBLE);
        llBottomBar.setVisibility(View.GONE);
        progressBar.setVisibility(View.GONE);
        viewDivider.setVisibility(View.GONE);
    }

    @Override
    public void showError() {
        ErrorUtils.showErrorFragment(R.id.fl_error_container, getSupportFragmentManager(), new ErrorUtils.doNextCallback() {
            @Override
            public void retry() {
                mPresenter.getCartBookList(UserUtil.getToken());
            }
        });
        tvRightTopBtn.setVisibility(View.GONE);
        recyclerView.setVisibility(View.GONE);
        progressBar.setVisibility(View.GONE);
        llBottomBar.setVisibility(View.GONE);
        viewDivider.setVisibility(View.GONE);
    }

    @Override
    public void showEmpty() {
        ErrorUtils.showEmpty(this, flErrorContainer, R.layout.tinder_empty_tips, new ErrorUtils.doNextCallback() {
            @Override
            public void moveTo() {
                SwipeActivity.startActivity(FavoritesActivity.this);
            }
        });
        tvRightTopBtn.setVisibility(View.GONE);
        recyclerView.setVisibility(View.GONE);
        progressBar.setVisibility(View.GONE);
        llBottomBar.setVisibility(View.GONE);
        viewDivider.setVisibility(View.GONE);
    }
}
